var rotate = function(nums, k) {
  k = k % nums.length;

  reverse(nums, 0, nums.length - 1);
  reverse(nums, 0, k - 1);
  reverse(nums, k, nums.length - 1);
  return nums;
}

function reverse(nums, start, end) {
  for (let i = start, j = end; i < j; i++, j--) {
    [nums[i], nums[j]] = [nums[j], nums[i]]
  }
  return nums;
}

const nums = [1,2,3,4,5,6,7], k = 3;
console.log(rotate(nums, k))